package controller

import (
	"fmt"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Active struct{}

func (*Active) CreateActive(c *gin.Context) {

	var article *request.ActiveReq

	c.ShouldBindJSON(&article)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ActiveService.CreateActive(claims.BaseClaims, article)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Active) EditActive(c *gin.Context) {

	var article *request.ActiveReq

	c.ShouldBindJSON(&article)

	fmt.Println(tools.JsonParse(article))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ActiveService.EditActive(claims.BaseClaims, article)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)
}

func (*Active) GetList(c *gin.Context) {

	var paginate request.ActiveSearchReq

	c.ShouldBind(&paginate)

	fmt.Println(paginate)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
	}

	data, err := services.ActiveService.GetList(claims.BaseClaims, &paginate)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
	}

	utils.OkWithData(data, c)
}

func (*Active) SearchActive(c *gin.Context) {
	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	data, err := services.ActiveService.GetActive(claims.BaseClaims, id)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(data, c)
}

func (*Active) Delete(c *gin.Context) {

	var IDS *request.IDS

	c.ShouldBindJSON(&IDS)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	err = services.ActiveService.DeleteAct(IDS.IDS, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

var ActiveController = new(Active)
